package it.polimi.authentication.ejb.usermanagement;

import it.polimi.authentication.ejb.usermanagement.dto.UserDTO;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.commons.codec.digest.DigestUtils;

/**
 * Entity implementation class for Entity: UserEntity
 *
 */
@Entity
@Table(name="user")
@NamedQueries({		@NamedQuery(name=User.FIND_ALL, query="SELECT u FROM User u ORDER BY u.registeredon ASC"),
					@NamedQuery(name=User.FIND_BY_MAIL, query="SELECT u FROM User u WHERE u.email = :email")
})
public class User implements Serializable {
	private static final long serialVersionUID = 1L;
	
	public static final String FIND_ALL = "User.findAll";
	
	public static final String FIND_BY_MAIL = "User.findByMail";
	   
	@Id
	private String email;
	
    private String name;
      
    private String surname;
	
	private String password;//sha-512 + hex
	
	@Temporal(javax.persistence.TemporalType.DATE)
	private Date birthdate;
	
	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date registeredon;
	
	@ElementCollection(targetClass = Group.class)
    @CollectionTable(name = "user_group",
                    joinColumns = @JoinColumn(name = "email"))
    @Enumerated(EnumType.STRING)
    @Column(name="groupname")
    private List<Group> groups;

	public User() {
		super();
	}
	
	public User(UserDTO user){
         
        this.email        = user.getEmail();
        this.name= user.getName();
        this.surname=user.getSurname();        
        this.birthdate=user.getBirthdate();
        this.password     = DigestUtils.sha512Hex(user.getPassword() );
        this.registeredon = new Date();
    }
	
 
    public String getEmail() {
        return email;
    }
  
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}
  
    
    public Date getBirthdate() {
		return this.birthdate;
	}

	public void setBirthdate(Date birthdate) {
		this.birthdate = birthdate;
	}
	
	
    /**
     * @return the password in SHA512 HEX representation
     */
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public Date getRegisteredOn() {
        return registeredon;
    }
 
    public void setRegisteredOn(Date registeredon) {
        this.registeredon = registeredon;
    }
    
 
    public List<Group> getGroups() {
        return groups;
    }
 
    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }
 
    @Override
    public String toString() {
        return "User [email=" + email + ", firstName=" + name
                + ", lastName=" + surname + ", password=" + password
                + ", registeredOn=" + registeredon + ", groups=" + groups + "]";
    }

}

	
